//
//  THHomeSegeViewController.m
//  TreeHole
//
//  Created by apple on 2020/7/27.
//  Copyright © 2020 stoneobs. All rights reserved.
//

#import "THTreeHoleHomeViewController.h"
#import "THTieZiSearchViewController.h"
#import "THTreeHoleChildRecomendViewController.h"
#import "THTreeHoleChildVideoViewController.h"
#import "THTreeHoleChildSendViewController.h"
#import "THTreeHoleChildAttentionViewController.h"
#import "THTreeHoleChildCollectViewController.h"
#import "THTreeHoleChildMineViewController.h"

@interface THTreeHoleHomeViewController ()<UIScrollViewDelegate>
@property(nonatomic, strong) STTools_SegementAutoView                     *segView;/**<  */
@end

@implementation THTreeHoleHomeViewController
- (UIStatusBarStyle)preferredStatusBarStyle{
    return UIStatusBarStyleDefault;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    UIView * topView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, UISCREEN_WIDTH, TMUtils.stoneobs_fun_get_navgationBarBootom)];
    topView.backgroundColor = UIColor.whiteColor;
    [self.view addSubview:topView];
    __weak typeof(self) weakSelf =  self;
    self.segView = [[STTools_SegementAutoView alloc] initWithFrame:CGRectMake(10, 0, UISCREEN_WIDTH -20 - 40, 44) andTitle:@[@"广场",@"视频",@"发布",@"关注",@"收藏",@"我的"] handle:^(STTools_SegementAutoView *sender, UIButton *currentSelctedButton) {
        
        [weakSelf.scrollView setContentOffset:CGPointMake(sender.cureentIndex*UISCREEN_WIDTH, 0) animated:YES];

    }];
    self.segView.bottom = self.cus_navgationBar.height;
    [topView addSubview:self.segView];
    self.segView.bottom = topView.height;
    self.segView.lineSelectedColor = UIColor.clearColor;
    self.segView.lineSelectedView.backgroundColor = UIColor.clearColor;
    self.segView.lineColor = UIColor.clearColor;
    self.segView.autoMoveWithClic = YES;
    
    self.segView.butTitleSelectedColor = UIColor.blackColor;
    self.segView.butTitleColor = WH_UICOLOR_FROM_RGBA(0x343434);
    self.segView.butTitleFont = [UIFont fontWithName:STONEOBS_UI_FONT_CU_NAME size:18];
    self.segView.butTitleSelectedFont = [UIFont fontWithName:STONEOBS_UI_FONT_CU_NAME size:24];
    
    UIImageView * stoneobs_ui_imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 16, 4)];
    [self.segView.lineSelectedView addSubview:stoneobs_ui_imageView];
    stoneobs_ui_imageView.layer.cornerRadius = 2;
    stoneobs_ui_imageView.clipsToBounds = YES;
    stoneobs_ui_imageView.centerX = self.segView.lineSelectedView.width/2;
    stoneobs_ui_imageView.centerY =  self.segView.lineSelectedView.height / 2;
    self.segView.lineSelectedView.clipsToBounds = NO;
    self.segView.lineSelectedView.top = self.segView.lineSelectedView.top-5;
    self.segView.lineSelectedView.hidden = YES;
    self.segView.clipsToBounds = NO;
    self.segView.butonWith = 65;
    [TMUtils stoneobs_fun_makeViewToThemeGrdualColor:stoneobs_ui_imageView];
    
    

    STButton * messageButton = [[STButton alloc] initWithFrame:CGRectMake(0, 0, 44, 44)
                                                         title:nil
                                                    titleColor:nil
                                                     titleFont:0
                                                  cornerRadius:0
                                               backgroundColor:nil
                                               backgroundImage:nil
                                                         image:[UIImage imageNamed:@"good_搜索"]];
    [topView addSubview:messageButton];
    [messageButton setClicAction:^(UIButton *sender) {
        [weakSelf pushViewController:THTieZiSearchViewController.new animated:1];
    }];
    messageButton.right = UISCREEN_WIDTH;
        messageButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
    [topView addSubview:messageButton];

    messageButton.bottom = topView.height;
    [messageButton stoneobs_fun_st_showLeftLine:14];
    self.cus_navgationBar.hidden = YES;

    [self stoneobs_ui_configSubView];
    // Do any additional setup after loading the view.
}

#pragma mark --configSubView
- (void)stoneobs_ui_configSubView{
    
    self.scrollView = [UIScrollView.alloc initWithFrame:CGRectMake(0, TMUtils.stoneobs_fun_get_navgationBarBootom, UISCREEN_WIDTH, TMUtils.stoneobs_fun_get_tabBarTop - TMUtils.stoneobs_fun_get_navgationBarBootom)];
    if (@available(iOS 11.0, *)) {
        self.scrollView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    } else {
        self.automaticallyAdjustsScrollViewInsets = NO;
    }
    [self.view addSubview:self.scrollView];
    self.scrollView.showsHorizontalScrollIndicator = NO;
    self.scrollView.pagingEnabled = YES;
    self.scrollView.delegate = self;
    self.scrollView.scrollEnabled = NO;
    NSInteger i = 0;
    ({
        THTreeHoleChildRecomendViewController * vc = THTreeHoleChildRecomendViewController.new;
        [self addChildViewController:vc];
        [self.scrollView addSubview:vc.view];
        vc.view.left = UISCREEN_WIDTH * i;

        i++;
    });
    ({
        THTreeHoleChildVideoViewController * vc = THTreeHoleChildVideoViewController.new;
        [self addChildViewController:vc];
        [self.scrollView addSubview:vc.view];
        vc.view.left = UISCREEN_WIDTH * i;

        i++;
    });
    ({
        THTreeHoleChildSendViewController * vc = THTreeHoleChildSendViewController.new;
        [self addChildViewController:vc];
        [self.scrollView addSubview:vc.view];
        vc.view.left = UISCREEN_WIDTH * i;

        i++;
    });
    ({
        THTreeHoleChildAttentionViewController * vc = THTreeHoleChildAttentionViewController.new;
        [self addChildViewController:vc];
        [self.scrollView addSubview:vc.view];
        vc.view.left = UISCREEN_WIDTH * i;

        i++;
    });
    ({
        THTreeHoleChildCollectViewController * vc = THTreeHoleChildCollectViewController.new;
        [self addChildViewController:vc];
        [self.scrollView addSubview:vc.view];
        vc.view.left = UISCREEN_WIDTH * i;

        i++;
    });
    ({
        THTreeHoleChildMineViewController * vc = THTreeHoleChildMineViewController.new;
        [self addChildViewController:vc];
        [self.scrollView addSubview:vc.view];
        vc.view.left = UISCREEN_WIDTH * i;

        i++;
    });
    self.scrollView.contentSize = CGSizeMake(UISCREEN_WIDTH * i, 0);
    self.scrollView.pagingEnabled = YES;
    self.scrollView.alwaysBounceVertical = NO;
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    if (scrollView == self.scrollView) {
        NSInteger index = scrollView.contentOffset.x / UISCREEN_WIDTH;
        self.segView.cureentIndex = index;
    }
}
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
    if (scrollView == self.scrollView) {
        NSInteger index = scrollView.contentOffset.x / UISCREEN_WIDTH;
        self.segView.cureentIndex = index;
    }
}
@end
